package ui;

import org.eclipse.swt.widgets.Composite;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.wb.swt.SWTResourceManager;

import each_class.SongClass;


//纯UI类没对外接口
public class PlaylistItem extends Composite/* implements MouseListener, MouseTrackListener*/{
	private List<SongClass> playList;
	private ScrolledView listView;
	private Button deleteButton;
	private MusicInfo musicInfo;
	private boolean isPlaying;
	

	public static PlaylistItem getCurrentPlay() {
		return currentPlay;
	}

	public static void setCurrentPlay(PlaylistItem currentPlay) {
		PlaylistItem.currentPlay = currentPlay;
	}

	private static PlaylistItem currentPlay;
	
	public PlaylistItem(Composite parent, SongClass music) {
		super(parent, SWT.NONE);
		listView = (ScrolledView) getParent();
		playList = ((PlayList)listView.getParent()).getPlayList();
		
		setSize(350, 60);
		deleteButton = new Button(this, SWT.NONE) {
			
			@Override
			public void mouseUp(MouseEvent e) {
				((PlayList)listView.getParent()).deleteMusic(music);
				getParent().dispose();
				listView.downcount -= listView.scrollHeight;
				listView.layout();
			}
		};
		deleteButton.setBounds(290, 11, 32, 32);
		deleteButton.initial(
				new Image(getDisplay(), "src/image/delete_x16_deactive.png"), 
				new Image(getDisplay(), "src/image/delete_x16_active.png"));
		
		musicInfo = new MusicInfo(this, SWT.NONE) {
			
			@Override
			public void mouseEnter(MouseEvent e) {
				if (!isPlaying) {
					setBackground(new Color(getDisplay(), 240, 240, 240));
					getParent().setBackground(new Color(getDisplay(),240, 240, 240));
					deleteButton.setVisible(true);
				}
			}
			@Override
			public void mouseExit(MouseEvent e) {
				if (isPlaying) {
					return;
				}
				Point size = getParent().getSize();
				if (e.x > size.x - 5 || e.y > size.y - 5 || e.x < 0 || e.y < 0) {
					setBackground(new Color(getDisplay(),250, 250, 250));
					getParent().setBackground(new Color(getDisplay(),250, 250, 250));
					deleteButton.setVisible(false);
				}
			}
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				((PlayList)listView.getParent()).play(playList.indexOf(music));
			}
		};
		musicInfo.initail(music);
		musicInfo.setBounds(0, 0, 290, 60);
		musicInfo.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		musicInfo.layout();
		musicInfo.addMouseWheelListener((MouseWheelListener) listView);
	
		
	}
	
	public void active() {
		isPlaying = true;
		currentPlay = this;
		musicInfo.setBackground(new Color(getDisplay(), 240, 240, 240));
		setBackground(new Color(getDisplay(),240, 240, 240));
		deleteButton.setVisible(true);
	}
	
	public void deactive() {
		isPlaying = false;
		musicInfo.setBackground(new Color(getDisplay(),250, 250, 250));
		setBackground(new Color(getDisplay(),250, 250, 250));
		deleteButton.setVisible(false);
	}

//	@Override
//	public void mouseEnter(MouseEvent e) {
//		setBackground(new Color(getDisplay(), 240, 240, 240));
//		musicInfo.setBackground(getBackground());
//		deleteButton.setVisible(true);
//	}
//
//	@Override
//	public void mouseExit(MouseEvent e) {
//		if (e.x > getSize().x || e.y > getSize().y - 5 || e.x < 0 || e.y < 0) {
//			setBackground(getParent().getBackground());
//			musicInfo.setBackground(getBackground());
//			deleteButton.setVisible(false);
//		}
//	}
//
//	@Override
//	public void mouseHover(MouseEvent e) {}
//
//	@Override
//	public void mouseDoubleClick(MouseEvent e) {
//		PlayList playList = (PlayList)listView.getParent();
//		int index = this.playList.indexOf(music);
//		playList.setCurrentMusic(index);
//		playList.play(index);
//	}
//
//	@Override
//	public void mouseDown(MouseEvent e) {}
//
//	@Override
//	public void mouseUp(MouseEvent e) {}
}
